﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PRE.catalog
{
    public partial class FrmTiepnhanbenh : PRE.catalog.FrmBase
    {
        BLL.PhieukhamBLL _bll = new BLL.PhieukhamBLL();
        DAL.PhieukhamDAL _dal = new DAL.PhieukhamDAL();
        public FrmTiepnhanbenh()
        {
            InitializeComponent();
        }


        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {

            ReadOnlyControl(false);
            ClearDataBindings();
            ChangeStatus_PhieuTT(false);

            //string _ma = _bll.CreateID();
            //txtMaBN.Text = _ma;

            txtsttphieu.ResetText();
            txtphikham.ResetText();
            //txtketluanbenh.ResetText();
            dtengaykham.EditValue = _bll.GetDate();
            radioBH.EditValue = false;
            cbomatrangthai.EditValue = -1;
            cbomabenhnhan.EditValue = -1;
            cbomakhoa.EditValue = -1;
            //cbomanv.EditValue = -1;
            cbosophong.EditValue = -1;

            txtsttphieu.Focus();



            base.PerformAdd();
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {

            var o = new DAL.Entities.phieukham()
            {
                sttphieu = txtsttphieu.Text.Trim(),
                ngaykham = Convert.ToDateTime(dtengaykham.EditValue)
            };

            DialogResult result;
            var ok = _dal.deleteobj(o);
            result = MessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                }
                else
                    MessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);
            ChangeStatus_PhieuTT(false);
            radioBH.Focus();
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            string stt = grvMain.GetFocusedRowCellValue("STTPHIEU") + "";
            DateTime ngay = Convert.ToDateTime(grvMain.GetFocusedRowCellValue("NGAYKHAM") + "");
            DataTable dtb_report = _dal.Select_Reporttiepnhanbenh(stt, ngay);

            var frm = new Report.Report_Base();
            var xr = new Report.XRTiepnhanbenhnhan();

            xr.DataSource = dtb_report;
            frm.SetReport(xr);

            frm.Show();
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();

            if (_dtb != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }

            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {


            if (IsAdd)
            {
                var o = new DAL.Entities.phieukham()
                {
                    sttphieu = txtsttphieu.Text.Trim(),
                    ngaykham = Convert.ToDateTime(dtengaykham.EditValue),
                    baohiem = Convert.ToBoolean(radioBH.EditValue),
                    phikham = float.Parse("0" + txtphikham.Text.Trim()),
                    ketluanbenh = "",
                    matrangthai = (cbomatrangthai.EditValue + "").Trim(),
                    manv =(cbomanv.EditValue+"").Trim(),
                    mabn = (cbomabenhnhan.EditValue + "").Trim(),
                    sophong = Convert.ToInt32("0" + (cbosophong.EditValue + "").Trim()),
                    makhoa = (cbomakhoa.EditValue + "").Trim(),
                    trangthaithanhtoan = false

                };

                var oki = _dal.Insert(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus_PhieuTT(false);
                    PerformAdd();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus_PhieuTT(false);
                    ReadOnlyControl(false);
                }

            }

            if (IsEdit)
            {
                //var _mabn = grvMain.GetFocusedRowCellValue("MaBN") + "";
                var o = new DAL.Entities.phieukham()
                {
                    sttphieu = txtsttphieu.Text.Trim(),
                    ngaykham = Convert.ToDateTime(dtengaykham.EditValue),
                    baohiem = Convert.ToBoolean(radioBH.EditValue),
                    phikham = float.Parse("0" + txtphikham.Text.Trim()),
                    ketluanbenh = "",
                    matrangthai = (cbomatrangthai.EditValue + "").Trim(),
                    manv = (cbomanv.EditValue+ "").Trim(),
                    mabn = (cbomabenhnhan.EditValue + "").Trim(),
                    sophong = Convert.ToInt32("0" + (cbosophong.EditValue + "").Trim()),
                    makhoa = (cbomakhoa.EditValue + "").Trim(),
                    trangthaithanhtoan = false

                };

                var oki = _dal.Update(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus_PhieuTT();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus_PhieuTT(false);
                    ReadOnlyControl(false);
                }
            }


            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {

            txtsttphieu.Text = null;
            txtphikham.Text = null;
            //txtketluanbenh.Text = "",
            dtengaykham.EditValue = _bll.GetDate();
            radioBH.EditValue = false;
            cbomanv.EditValue = -1;
            cbomabenhnhan.EditValue = -1;
            cbomatrangthai.EditValue = -1;
            cbosophong.EditValue = -1;
            cbomakhoa.EditValue = -1;
            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            txtsttphieu.DataBindings.Clear();
            txtphikham.DataBindings.Clear();
            dtengaykham.DataBindings.Clear();
            radioBH.DataBindings.Clear();
            //txtketluanbenh.DataBindings.Clear();

            cbomanv.DataBindings.Clear();
            cbomabenhnhan.DataBindings.Clear();
            cbomatrangthai.DataBindings.Clear();
            cbosophong.DataBindings.Clear();
            cbomakhoa.DataBindings.Clear();


            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {
            txtsttphieu.DataBindings.Add("EditValue", _dtb, ".STTPHIEU");
            txtphikham.DataBindings.Add("EditValue", _dtb, ".PHIKHAM");
            dtengaykham.DataBindings.Add("EditValue", _dtb, ".NGAYKHAM");
            radioBH.DataBindings.Add("EditValue", _dtb, ".BAOHIEM");
            //txtketluanbenh.DataBindings.Add("EditValue", _dtb, ".KETLUANBENH");

            cbomanv.DataBindings.Add("EditValue", _dtb, ".MA_NV");
            cbomabenhnhan.DataBindings.Add("EditValue", _dtb, ".MA_BN");
            cbomatrangthai.DataBindings.Add("EditValue", _dtb, ".MATRANGTHAI");
            cbosophong.DataBindings.Add("EditValue", _dtb, ".SOPHONG");
            cbomakhoa.DataBindings.Add("EditValue", _dtb, ".MAKHOA");


            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            //txtMaHV.Properties.ReadOnly = isReadOnly;
            txtsttphieu.Properties.ReadOnly = isReadOnly;
            dtengaykham.Properties.ReadOnly = isReadOnly;
            radioBH.Properties.ReadOnly = isReadOnly;
            txtphikham.Properties.ReadOnly = isReadOnly;
            //txtketluanbenh.Properties.ReadOnly = isReadOnly;

            cbomanv.Properties.ReadOnly = isReadOnly;
            cbomabenhnhan.Properties.ReadOnly = isReadOnly;
            cbomatrangthai.Properties.ReadOnly = isReadOnly;
            cbosophong.Properties.ReadOnly = isReadOnly;
            cbomakhoa.Properties.ReadOnly = isReadOnly;

            grcMain.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        protected override void PerformSearch()
        {
            base.PerformSearch();
        }

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            //DataTable tmp = _dal3.Select_Current(DateTime.Now);
            //string _makhoa = tmp.Rows[0]["Makhoa"] + "";
            _dtb = _dal.Select();


            if (_dtb != null)
            {
                grcMain.DataSource = _dtb;
            }

            base.LoadData();
        }
        #endregion

       

        private void FrmTiepnhanbenh_Load(object sender, EventArgs e)
        {
            DataTable _dtb1 = _dal.Select_nhanvien();
            cbomanv.Properties.DataSource = _dtb1;

            DataTable _dtb2 = _dal.Select_benhnhan();
            cbomabenhnhan.Properties.DataSource = _dtb2;

            DataTable _dtb3 = _dal.Select_trangthaikham();
            cbomatrangthai.Properties.DataSource = _dtb3;

            DataTable _dtb4 = _dal.Select_phong();
            cbosophong.Properties.DataSource = _dtb4;

            DataTable _dtb5 = _dal.Select_khoa();
            cbomakhoa.Properties.DataSource = _dtb5;
            ChangeStatus_PhieuTT();
            PerformRefresh();
        }

        private void cbomakhoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (IsAdd)
            {
                string a = (cbomakhoa.EditValue + "").Trim();
                cbosophong.Properties.DataSource = _dal.Select_phongtheokhoa(a);
            }
        }

        private void cbomakhoa_EditValueChanged(object sender, EventArgs e)
        {
            if (IsAdd)
            {
                string a = (cbomakhoa.EditValue + "").Trim();
                cbosophong.Properties.DataSource = _dal.Select_phongtheokhoa(a);
            }
        }
    }
}
